package toolkits;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpConnectionParams;
import org.apache.commons.httpclient.params.HttpMethodParams;

/**
 * http.connection.timeout: 
 *   not set: throw java.net.ConnectException: Connection timed out: connect in about 84 seconds
 *   set: org.apache.commons.httpclient.ConnectTimeoutException: The host did not accept the connection within timeout of 5000 ms
 *        Caused by: java.net.SocketTimeoutException: connect timed out
 * 
 * http.socket.timeout
 *   set: java.net.SocketTimeoutException: Read timed out
 * @author wei
 *
 */
public class HttpClientTest {
    public static HttpClient hc = new HttpClient();
    /**
     * @param args
     */
    public static void main(String[] args) {
        
        out("################# CALL A");
        try {
            testSSLWithout();
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        out("################# CALL B");
        try {
            testSSL();
        } catch (Exception e) {
            
            e.printStackTrace();
        }
    }
    
    public static void testSSLWithout() throws Exception{
        /* if the first call do not set it then this line doesn't take effect!!! */
        System.setProperty("javax.net.debug", "ssl");
        List<NameValuePair> nvplist=new ArrayList<NameValuePair>();
        nvplist.add(new NameValuePair("command", "verify_payment"));
        NameValuePair[] nvps = new NameValuePair[nvplist.size()];
        out(post("https://localhost/test.jsp", nvplist.toArray(nvps)));
    }
    
    public static void testWithout() throws Exception{
        List<NameValuePair> nvplist=new ArrayList<NameValuePair>();
        nvplist.add(new NameValuePair("command", "verify_payment"));
        NameValuePair[] nvps = new NameValuePair[nvplist.size()];
        out(post("http://localhost/test.jsp", nvplist.toArray(nvps)));
    }
    
    public static void testSSL() throws Exception{
        out("XXXXXXXXXXXX!!!   "+System.class.getClassLoader());
        System.setProperty("javax.net.ssl.keyStoreType", "pkcs12");
        System.setProperty("javax.net.ssl.keyStore", "E:\\openssl\\client1.p12"); 
        System.setProperty("javax.net.ssl.keyStorePassword", "1234");
        //System.out.println(System.getProperties());
//        System.setProperty("javax.net.ssl.keyStoreType", "pkcs12");
//        System.setProperty("javax.net.ssl.keyStore", "E:\\openssl\\client2.p12"); 
//        System.setProperty("javax.net.ssl.keyStorePassword", "1234");
        
        //can not work
//        System.setProperty("javax.net.ssl.keyStoreType", "jks");
//        System.setProperty("javax.net.ssl.keyStore", "D:\\My Documents\\Downloads\\openssl\\client2.keystore"); 
//        System.setProperty("javax.net.ssl.keyStorePassword", "12345678");
        
        
        
        //do not set global trust ca
//        System.setProperty("javax.net.ssl.trustStoreType", "jks");
//        System.setProperty("javax.net.ssl.trustStore", "D:\\My Documents\\Downloads\\openssl\\wei.keystore");
//        System.setProperty("javax.net.ssl.trustStorePassword", "12345678");
        
        //System.setProperty("javax.net.debug", "ssl");
        List<NameValuePair> nvplist=new ArrayList<NameValuePair>();
        nvplist.add(new NameValuePair("command", "verify_payment"));
        NameValuePair[] nvps = new NameValuePair[nvplist.size()];
        out(post("https://localhost/test.jsp", nvplist.toArray(nvps)));
    }
    
    public static void testPayU(){
        hc.getParams().setParameter(HttpConnectionParams.CONNECTION_TIMEOUT, new Integer(15000));
        hc.getParams().setParameter(HttpConnectionParams.SO_TIMEOUT, new Integer(60000));
        List<NameValuePair> nvplist=new ArrayList<NameValuePair>();
        nvplist.add(new NameValuePair("command", "verify_payment"));
        NameValuePair[] nvps = new NameValuePair[nvplist.size()];
        
        long start = System.currentTimeMillis();
        long end;
        try {
            //https://test.payu.in/merchant/postservice?form=2
            //http://127.0.0.1/pcisecure/pcisecureorderform.html
            post("https://test.payu.in/merchant/postservice?form=2", nvplist.toArray(nvps));
            end = System.currentTimeMillis();
        } catch (Exception e) {
            end = System.currentTimeMillis();
            out(e.getClass().getName());
            e.printStackTrace();
        }
        out( (end - start)/1000 );
        
        out(HttpConnectionParams.CONNECTION_TIMEOUT+"="+hc.getParams().getParameter(HttpConnectionParams.CONNECTION_TIMEOUT));
        out(HttpConnectionParams.SO_TIMEOUT+"="+hc.getParams().getParameter(HttpConnectionParams.SO_TIMEOUT));
        out(HttpMethodParams.RETRY_HANDLER+"="+hc.getParams().getParameter(HttpMethodParams.RETRY_HANDLER));
    }
    
    public static void out(Object obj){
        if(obj == null){
            System.out.println("!!!!!!!! "+"EMPTY");
        }else{
            System.out.println("!!!!!!!! "+obj.toString());
        }
    }
    
    public static String post(String url,NameValuePair... nvps)throws Exception
    {
        PostMethod post=new PostMethod(url);
        out(HttpMethodParams.RETRY_HANDLER+"="+post.getParams().getParameter(HttpMethodParams.RETRY_HANDLER));
        try {
            post.setRequestBody(nvps);
            int statusCode = hc.executeMethod(post);
            if (statusCode == HttpStatus.SC_OK)
                return post.getResponseBodyAsString();
        } finally{
            post.releaseConnection();
        } 

        return null;
    }

}
